<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '上传' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input v-model="dataForm.fileName" placeholder="文件名"></el-input>
      </el-form-item>
      <el-form-item label="文件路径" prop="filePath">
        <el-input v-model="dataForm.filePath" placeholder="文件路径"></el-input>
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-input v-model="dataForm.fileType" placeholder="文件类型"></el-input>
      </el-form-item>
      <el-form-item label="生成的文件名" prop="genFileName">
        <el-input v-model="dataForm.genFileName" placeholder="生成后的文件名"></el-input>
      </el-form-item>
      <el-form-item label="文件">
        <el-upload
            ref="upload"
            class="upload-demo"
            :limit="1"
            :on-change="handleFileChange"
            :on-exceed="handleExceed"
            :auto-upload="false"
        >
          <template #trigger>
            <el-button type="primary">select file</el-button>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {reactive, ref} from 'vue'
import {ElMessage, genFileId, UploadFile} from 'element-plus/es'
import {useTemplateFileInfoApi, useTemplateFileSubmitApi} from '@/api/templateFile'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
import {ITemplateFileForm} from "@/types/templateFile";

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

// 模板表单
const dataForm: ITemplateFileForm = reactive({
  id: '',
  title: '',
  fileName: '',
  filePath: '',
  fileType: '',
  groupId: '',
  genFileName: '',
  file: undefined
})

const init = (groupId: number | string, id?: number) => {
  visible.value = true
  // 设置组 ID
  dataForm.groupId = groupId
  // 数据 ID
  dataForm.id = id

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    getFile(id)
  }
}

const upload = ref<UploadInstance>()

// 限制只上传一个文件
const handleExceed: UploadProps['onExceed'] = (files) => {
  upload.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

const getFile = (id: number | string) => {
  useTemplateFileInfoApi(id).then(res => {
    Object.assign(dataForm, res.data)
  })
}

// 文件上传改变
const handleFileChange = (file: UploadFile) => {
  // 赋值文件
  dataForm.file = file.raw
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    useTemplateFileSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          dataForm.file = undefined
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
